using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraLimit_Ctrl : MonoBehaviour
{
    public bool isLocal = true;
    public bool isNull;
    public Vector2 Pos_0;
    public Vector2 Pos_1;
    public Vector2 minPos
    {
        get
        {
            return new Vector3(Pos_0.x > Pos_1.x ? Pos_1.x : Pos_0.x, Pos_0.y > Pos_1.y ? Pos_1.y : Pos_0.y) + (isLocal ? this.transform.position : Vector3.zero);
        }
    }
    public Vector2 maxPos
    {
        get
        {
            return new Vector3(Pos_0.x < Pos_1.x ? Pos_1.x : Pos_0.x, Pos_0.y < Pos_1.y ? Pos_1.y : Pos_0.y) + (isLocal ? this.transform.position : Vector3.zero);
        }
    }

    public bool isGlobalGizmos = false;
    private void OnDrawGizmosSelected()
    {
        if (!isGlobalGizmos&& !isNull)
        {
            Gizmos.color = Color.blue;
            Gizmos.DrawLine(minPos, new Vector3(minPos.x, maxPos.y));
            Gizmos.DrawLine(minPos, new Vector3(maxPos.x, minPos.y));
            Gizmos.DrawLine(maxPos, new Vector3(minPos.x, maxPos.y));
            Gizmos.DrawLine(maxPos, new Vector3(maxPos.x, minPos.y));
        }
    }
    private void OnDrawGizmos()
    {
        if (isGlobalGizmos && !isNull)
        {
            Gizmos.color = Color.blue;
            Gizmos.DrawLine(minPos, new Vector3(minPos.x, maxPos.y));
            Gizmos.DrawLine(minPos, new Vector3(maxPos.x, minPos.y));
            Gizmos.DrawLine(maxPos, new Vector3(minPos.x, maxPos.y));
            Gizmos.DrawLine(maxPos, new Vector3(maxPos.x, minPos.y));
        }
    }
    [ContextMenu("Init")]
    public void Init()
    {
        BoxCollider2D boxCollider2D = this.GetComponent<BoxCollider2D>();
        if (boxCollider2D != null)
        {
            isLocal = true;
            Pos_0.x = -boxCollider2D.size.x * 0.5f+ boxCollider2D.offset.x;
            Pos_1.x = boxCollider2D.size.x * 0.5f + boxCollider2D.offset.x;
            Pos_0.y = -boxCollider2D.size.y * 0.5f + boxCollider2D.offset.y;
            Pos_1.y = boxCollider2D.size.y * 0.5f + boxCollider2D.offset.y;
        }
    }
}
